<?php

class BudgetController extends Controller
{


    /*
    * 获取房屋种类列表
    *
    */
    public function actionGetHouseTypeList()
    {
        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //获取列表
            $searchsql = Util::initSearchSql();



            //排序
           // $searchsql = $searchsql . " order by publish_time  desc";
            //获取list
            $list = BudgetHousetype ::model()->findAll($searchsql);


                //返回数据
            Util::responseList(count($list), $list);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }



    /*
    * 获取单位列表
    *
    */
    public function actionGetUnitListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();


            //获取count
            $totalcount = BudgetUnit ::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time  desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = BudgetUnit::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 添加单位
    *
    */
    public function actionAddUnit()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");


            //是否存在
            $obj = BudgetUnit::model()->find("name='".$name."'");
            if(empty($obj)==false)
            {
                if(Util::isDelete($obj)==false)
                    throw new Exception("有同名的存在");
            }

            if(empty($obj)) {
                //添加
                $obj = new BudgetUnit();
                Util::initObjData($obj);


                $obj->name = $name;
            }
            else
            {
                Util::recoverObjData($obj);
            }


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDeleteUnit()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = BudgetUnit::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    //材料类别

    /*
    * 获取列表
    *
    */
    public function actionGetMaterialTypeList()
    {
        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $select_flag = Util::getParam("select_flag",0);


            $returnlist = array();
            //获取列表
            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStrAdd($searchsql,"level","1");



            //获取list
            $list = BudgetMaterialType ::model()->findAll($searchsql);


            for($i=0;$i<count($list);$i++)
            {
                $obj = $list[$i];

                $searchsql = Util::initSearchSql();
                $searchsql = Util::getSerarchStrAdd($searchsql,"level","2");
                $searchsql = Util::getSerarchStrAdd($searchsql,"parent_id",$obj->id);

                $slist = BudgetMaterialType::model()->findAll($searchsql);

                if($select_flag == 0)
                    array_push($returnlist,$obj);
                else
                {
                    if(count($slist)==0)
                        array_push($returnlist,$obj);


                }




                for($t=0;$t<count($slist);$t++)
                {
                    $objlist =$slist[$t];
                    if($select_flag == 1)
                        $objlist->name = $obj->name."-". $objlist->name;


                    array_push($returnlist,$objlist);
                }


            }


                //返回数据
                Util::responseList(count($returnlist), $returnlist);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    //材料列表
    /*
    * 获取列表分页
    *
    */
    public function actionGetMaterialListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();


            //获取count
            $totalcount = BudgetMaterial::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by id  desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = BudgetMaterial ::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    //材料


    /*
    * 添加材料
    *
    */
    public function actionAddMaterial()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");
            $type_id = Util::getParamNotEmpty("type_id");
            $unit_id = Util::getParamNotEmpty("unit_id");
            $formula = Util::getParamNotEmpty("formula");
            $ratio = Util::getParamNotEmpty("ratio");
            $remark = Util::getParam("remark", "");


            //单位
            $unit = BudgetUnit::model()->findByPK($unit_id);
            Util::checkExistAndNotDelete($unit);

            //种类
            $type = BudgetMaterialType::model()->findByPK($type_id);
            Util::checkExistAndNotDelete($type);


            //是否存在
            $obj = BudgetMaterial::model()->find("name='".$name."'");
            if(empty($obj)==false)
            {
                if(Util::isDelete($obj)==false)
                    throw new Exception("有同名的材料存在");
            }

            if(empty($obj)) {
                //添加
                $obj = new BudgetMaterial();
                Util::initObjData($obj);


            }
            else
            {
                Util::recoverObjData($obj);
            }



            $obj->name = $name;
            $obj->unit_id = $unit_id;
            $obj->type_id = $type_id;
            $obj->remark = $remark;
            $obj->ratio = $ratio;
            $obj->formula = $formula;
            $obj->unit_name = $unit->name;
            $obj->type_name = $type->name;


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 删除
    */
    public function actionDeleteMaterial()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = BudgetMaterial ::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    //案例列表
    /*
    * 获取列表分页
    *
    */
    public function actionGetCaseListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();


            //获取count
            $totalcount = BudgetCase::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by id  desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list =BudgetCase ::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 获取案例列表分页
    *
    */
    public function actionGetBudgetCaseListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();


            //获取count
            $totalcount = BudgetCase ::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time  desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = BudgetCase ::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 获取  详情列表
    *
    */
    public function actionGetDetailListPaging()
    {

        try {
            //参数验证
            $case_id = Util::getParamNotEmpty("case_id");

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();
            $searchsql  = Util::getSerarchStrAdd($searchsql,"case_id",$case_id);


            //获取count
            $totalcount = BudgetCaseDetail ::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time  desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list =BudgetCaseDetail  ::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

}